/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.mime.ParsedContact;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ModifyContact
/*     */   extends RedoableOp
/*     */ {
/*     */   private int mId;
/*     */   private Map<String, String> mFields;
/*     */   private ParsedContact mParsedContact;
/*     */   private RedoableOpData mRedoLogContent;
/*     */   
/*     */   public ModifyContact()
/*     */   {
/*  50 */     super(MailboxOperation.ModifyContact);
/*  51 */     this.mId = 0;
/*     */   }
/*     */   
/*     */   public ModifyContact(int mailboxId, int id, ParsedContact pc) {
/*  55 */     this();
/*  56 */     setMailboxId(mailboxId);
/*  57 */     this.mId = id;
/*  58 */     this.mFields = pc.getFields();
/*  59 */     this.mParsedContact = pc;
/*     */   }
/*     */   
/*     */   protected String getPrintableData()
/*     */   {
/*  64 */     StringBuffer sb = new StringBuffer("id=");
/*  65 */     sb.append(this.mId);
/*  66 */     if ((this.mFields != null) && (this.mFields.size() > 0)) {
/*  67 */       sb.append(", attrs={");
/*  68 */       for (Map.Entry<String, String> entry : this.mFields.entrySet()) {
/*  69 */         String key = (String)entry.getKey();
/*  70 */         String value = (String)entry.getValue();
/*  71 */         sb.append("\n    ").append(key).append(": ").append(value);
/*     */       }
/*  73 */       sb.append("\n}");
/*     */     }
/*  75 */     return sb.toString();
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException
/*     */   {
/*  80 */     out.writeInt(this.mId);
/*     */     
/*  82 */     int numAttrs = this.mFields != null ? this.mFields.size() : 0;
/*  83 */     out.writeShort((short)numAttrs);
/*  84 */     if (numAttrs > 0) {
/*  85 */       for (Map.Entry<String, String> entry : this.mFields.entrySet()) {
/*  86 */         out.writeUTF((String)entry.getKey());
/*  87 */         String value = (String)entry.getValue();
/*  88 */         out.writeUTF(value != null ? value : "");
/*     */       }
/*     */     }
/*  91 */     if (getVersion().atLeast(1, 14)) {
/*  92 */       out.writeShort((short)-1);
/*  93 */       out.writeInt((int)this.mParsedContact.getSize());
/*     */     }
/*     */   }
/*     */   
/*     */   public InputStream getAdditionalDataStream() throws IOException
/*     */   {
/*  99 */     if (getVersion().atLeast(1, 14)) {
/* 100 */       if (this.mParsedContact != null)
/* 101 */         return this.mParsedContact.getContentStream();
/* 102 */       if (this.mRedoLogContent != null) {
/* 103 */         return this.mRedoLogContent.getInputStream();
/*     */       }
/*     */     }
/* 106 */     return null;
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException
/*     */   {
/* 111 */     this.mId = in.readInt();
/* 112 */     if (!getVersion().atLeast(1, 14))
/* 113 */       in.readBoolean();
/* 114 */     int numAttrs = in.readShort();
/* 115 */     if (numAttrs > 0) {
/* 116 */       this.mFields = new HashMap(numAttrs);
/* 117 */       for (int i = 0; i < numAttrs; i++) {
/* 118 */         String key = in.readUTF();
/* 119 */         String value = in.readUTF();
/* 120 */         this.mFields.put(key, value);
/*     */       }
/*     */     }
/* 123 */     if (getVersion().atLeast(1, 14)) {
/* 124 */       in.readShort();
/* 125 */       int length = in.readInt();
/* 126 */       if (length > 0) {
/* 127 */         this.mRedoLogContent = new RedoableOpData(new File(in.getPath()), in.getFilePointer(), length);
/*     */         
/*     */ 
/* 130 */         long pos = in.getFilePointer();
/* 131 */         int numSkipped = in.skipBytes(length);
/* 132 */         if (numSkipped != length) {
/* 133 */           String msg = String.format("Attempted to skip %d bytes at position %d in %s, but actually skipped %d.", new Object[] { Integer.valueOf(length), Long.valueOf(pos), in.getPath(), Integer.valueOf(numSkipped) });
/*     */           
/* 135 */           throw new IOException(msg);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void redo() throws ServiceException
/*     */   {
/* 143 */     Mailbox mailbox = MailboxManager.getInstance().getMailboxById(getMailboxId());
/*     */     
/* 145 */     InputStream in = null;
/*     */     try {
/* 147 */       in = getAdditionalDataStream();
/* 148 */       ParsedContact pc = new ParsedContact(this.mFields, in);
/* 149 */       mailbox.modifyContact(getOperationContext(), this.mId, pc);
/*     */     } catch (IOException e) {
/* 151 */       throw ServiceException.FAILURE("Redo error", e);
/*     */     } finally {
/* 153 */       ByteUtil.closeStream(in);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/ModifyContact.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */